/******************************************************************************
**
** File name: 	utimer.h
**
** Description:	RTOS Unit Test System Library. This module implements timer-
**				counter configuration that is required to simulate periodic HW 
**				interrupts.
**
** Note:		Targets supported that are currently supported by this module:
**
**				- ATXmega32D4 (AVRSIM)		TCC0, TCD0, TCE0 
**
**				TBD: Add static configuration of the target HW. 
**				TBD: Add actual HW support. 
**
*******************************************************************************
** Change history:
**	
** 11/20/2011 - AK - File created. 
** 19/03/2012 - AK - Adopted for AVR Studio V5.1. 
** 
******************************************************************************/
#ifndef UTIMER_H__
#define UTIMER_H__

#define SYSTEM_TIMER_VECTOR 	TCC0_OVF_vect
#define USER_TIMER_1_VECTOR   	TCD0_OVF_vect		
#define USER_TIMER_2_VECTOR   	TCE0_OVF_vect

#define SYSTEM_TIMER			TCC0
#define USER_TIMER_1			TCD0				
#define USER_TIMER_2			TCE0

/* UTIMER module types defs */

typedef enum UTST_TLV_e
{
	LEVEL_LOW		=	TC_OVFINTLVL_LO_gc,
	LEVEL_MED		= 	TC_OVFINTLVL_MED_gc,
	LEVEL_HIGH		= 	TC_OVFINTLVL_HI_gc
}
UTST_TLV_t, * UTST_TLV_ptr;

typedef enum UTST_TST_e
{
	STATE_RUN,							// timer starts immediately after initialization
	STATE_STOP							// timer needs to call UTST_TIMER_START() to start
}
UTST_TST_t, * UTST_TST_ptr;

typedef struct UTST_TIMER_s
{
	TC0_t *				addr;
	unsigned short 		period;			// 0...65535 ticks ( actual interval depends on input frequency )
	UTST_TLV_t	 		level;			// LOW, MED, HI
	UTST_TST_t 			state;			// STOP, RUN
}
UTST_TIMER_t, * UTST_TIMER_ptr;

/* UTIMER module macro defs */

#define UTST_TIMER_START( __tmr__ ) 	\
	__tmr__.CTRLA = ( __tmr__.CTRLA & ~TC0_CLKSEL_gm ) | TC_CLKSEL_DIV1_gc
	
		
#define UTST_TIMER_STOP( __tmr__ )		\
	__tmr__.CTRLA = ( __tmr__.CTRLA & ~TC0_CLKSEL_gm ) | TC_CLKSEL_OFF_gc

/* UTIMER module function decs */

extern void utst_init_timer(UTST_TIMER_t * tmr);

#endif /* UTIMER_H__ */